package com.qf.controller.caiwu_controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Account;
import com.qf.bean.AccountItem;
import com.qf.bean.PrefundRule;
import com.qf.service.caiwu_service.AccountItemService;
import com.qf.service.caiwu_service.AccountService;
import com.qf.utils.RandomUtil;
import com.qf.utils.caiwu.form.AccountForm;
import com.qf.utils.caiwu.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/11 0011
 * Author->DT
 * sassspringboot
 * 行到水穷处 坐看云起时
 * 模块：账目模块
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountItemService accountItemService;
    @Autowired
    private AccountService accountService;
    //跳转到账目添加页面
    @RequestMapping("/add-ui")
    public String addUI(Model model){
        //查询出全部事项,放到请求域中
        List<AccountItem> itemList=accountItemService.selectAll();
        model.addAttribute("itemList", itemList);
        return "/caiwu/account-add";
    }

    //记账
    @RequestMapping("/add")
    @ResponseBody
    public ResultVO add(Account account){
        int count = accountService.insertSelective(account);
        //返回
        if (count==1){
            return new ResultVO(0, "记账成功成功！", null);
        }
        return new ResultVO(1, "记账失败！", null);
    }

    //跳转展示账目信息列表
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer index,
                       Model model, AccountForm accountForm){
        //0.封装参数
        Map map=new HashMap();
        map.put("index", index);
        map.put("page", 3);
        map.put("form", accountForm);
        //1.分页查询账目信息列表
        PageInfo<Account> pageInfo = accountService.findAccountListByLimit(map);
        //查询出全部事项,放到请求域中
        List<AccountItem> itemList=accountItemService.selectAll();
        model.addAttribute("itemList", itemList);
        //2.将查询到的信息放入Request域中
        model.addAttribute("pageInfo", pageInfo);
        //3.数据回显信息放入Request域中
        model.addAttribute("accountForm",accountForm);
        return "/caiwu/account-list";
    }

    //跳转账目编辑页面
    @RequestMapping("/edit-ui")
    public String editUI(Integer aid,Model model){
        //查询当前id对应的账目信息
        Account account = accountService.selectByPrimaryKey(aid);
        model.addAttribute("account", account);
        //查询出全部事项,放到请求域中
        List<AccountItem> itemList=accountItemService.selectAll();
        model.addAttribute("itemList", itemList);
        return "/caiwu/account-edit";
    }

    //编辑账目信息
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(Account account){
        //执行编辑
        int count = accountService.updateByPrimaryKeySelective(account);
        if (count==1){
            return new ResultVO(0, "修改账目信息成功！", null);
        }
        return new ResultVO(1, "修改账目信息失败！", null);
    }

    //删除指定账目信息
    @RequestMapping("/delete")
    @ResponseBody
    public ResultVO delete(Integer aid){
        //执行删除
        int count = accountService.deleteByPrimaryKey(aid);
        //返回
        if (count==1){
            return new ResultVO(0, "删除账目信息成功！", null);
        }
        return new ResultVO(1, "删除账目信息失败！", null);
    }

    //审核账目信息
    @RequestMapping("/check")
    @ResponseBody
    public ResultVO check(Integer aid){
        //执行审核
        int count = accountService.checkStateToChecked(aid);
        //返回
        if (count==1){
            return new ResultVO(0, "账目信息审核通过！", null);
        }
        return new ResultVO(1, "账目信息审核失败！", null);
    }

    //确认账目信息
    @RequestMapping("/affirm")
    @ResponseBody
    public ResultVO affirm(Integer aid){
        //修改确认状态
        int count = accountService.tradeStateToAffirm(aid);
        //返回
        if (count==1){
            return new ResultVO(0, "账目信息已确认！", null);
        }
        return new ResultVO(1, "账目信息确认失败！", null);
    }

//    //多行删除+导出数据
//    @RequestMapping("/delete-export")
//    public void deleteChecked(Integer[] aid, HttpServletResponse response, String method){
//        //method相当于鉴别器，根据传入的method不同，进行不同处理
//        //构建响应流，给用户提示，返回类型必需为void
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter writer = null;
//
//        //调用Service执行批量删除
//        if (method.equals("delete")){
//            int count = accountService.deleteChecked(aid);
//            //判断删除是否成功
//            try {
//                writer = response.getWriter();
//                if (count>=1){
//                    //删除成功
//                    writer.print("<script>alert('删除成功');location.href='/account/list'</script>");
//
//                }else {
//                    //删除失败
//                    writer.print("<script>alert('删除失败');location.href='/account/list'</script>");
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//        //将用户id对应的用户数据导出为Excel
//        else if (method.equals("export")){
//            //查询被选中id的用户信息
//            List<Account> accountList=accountService.findByIds(aid);
//            //利用工具类导出
//            //构建第一列
//            ExportExcelUtils.createFirst("账目信息");
//            //构建其他列
//            ExportExcelUtils.createOthers(accountList);
//            //构建输出流，导出到指定位置
//            try {
//                //随机生成文件名
//                String fileName= RandomUtil.dataRandom()+".xls";
//                //导出Excel
//                ExportExcelUtils.export(new FileOutputStream("f://"+fileName));
//                //导出成功,已下载框的方式响应给页面
//                //设置响应头，控制浏览器下载该文件
//                response.setContentType("application/x-download");
//                response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
//                ExportExcelUtils.export(response.getOutputStream());
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }
}
